﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using UserCtrl;

namespace GRWL.BZSC.UI
{
    public partial class SelectScdFrm : BaseFrm
    {
        public SelectScdFrm()
        {
            InitializeComponent();

            this.BaseDataProxy = new GRWL.BZSC.UI.BaseDataSvc.BaseDataClient();
            this.BaseDataProxy.Open();

        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            SearchBills();
        }

        private void SearchBills()
        {
            DataTable tbResult = null;

            QProduction[] tbpro = BaseDataProxy.FindProductions(this.txtScd.Text, null, null, edtBeginDate.Value, edtEndDate.Value, null, null, null,null);
            tbResult = tbpro.ToDataTable();

            #region
            if (tbpro != null)
            {
                this.dtpData.Rows.Clear();
                for (int i = 0; i < tbpro.Count(); i++)
                {
                    int j = dtpData.Rows.Add();
                    this.dtpData.Rows[j].Cells["id"].Value = tbpro[i].DetailId;
                    this.dtpData.Rows[j].Cells["code"].Value = tbpro[i].Code;
                    this.dtpData.Rows[j].Cells["orderCode"].Value = tbpro[i].OrderCode;
                    this.dtpData.Rows[j].Cells["pDate"].Value = Convert.ToDateTime(tbpro[i].PrdnDate);
                    this.dtpData.Rows[j].Cells["materialCode"].Value = tbpro[i].MaterialCode;
                    this.dtpData.Rows[j].Cells["materialName"].Value = tbpro[i].MaterialName;
                    this.dtpData.Rows[j].Cells["specs"].Value = tbpro[i].Specs;
                    this.dtpData.Rows[j].Cells["FinishNum"].Value = tbpro[i].FinishNum;
                }
            }
            #endregion
        }

        private void dtpData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            this.dtpData.Rows[e.RowIndex].HeaderCell.Value = Convert.ToString(e.RowIndex + 1);//显示行号，也可以设置成显示其他信息 
        }

        private void dtpData2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            this.dtpData2.Rows[e.RowIndex].HeaderCell.Value = Convert.ToString(e.RowIndex + 1);//显示行号，也可以设置成显示其他信息 
        }

        #region 选择/全选
        private void dtpData_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex >= 0)
            {
                try
                {
                    DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)(dtpData.Rows[e.RowIndex].Cells[0]);
                    if (c.Value != null && (bool)c.Value)
                    {
                        ((DataGridViewCheckBoxCell)(dtpData.Rows[e.RowIndex].Cells[0])).Value = false;
                    }
                    else
                    {
                        ((DataGridViewCheckBoxCell)(dtpData.Rows[e.RowIndex].Cells[0])).Value = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dtpData.RowCount; i++)
            {
                ((DataGridViewCheckBoxCell)(dtpData.Rows[i].Cells[0])).Value = cbAll.Checked;
            }
        }

        private void dtpData2_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex >= 0)
            {
                try
                {
                    DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)(dtpData2.Rows[e.RowIndex].Cells[0]);
                    if (c.Value != null && (bool)c.Value)
                    {
                        ((DataGridViewCheckBoxCell)(dtpData2.Rows[e.RowIndex].Cells[0])).Value = false;
                        //this.dtpData2.Rows[e.RowIndex].Selected = false;
                    }
                    else
                    {
                        ((DataGridViewCheckBoxCell)(dtpData2.Rows[e.RowIndex].Cells[0])).Value = true;
                        //this.dtpData2.Rows[e.RowIndex].Selected = true;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void cbCheck2_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dtpData2.RowCount; i++)
            {
                ((DataGridViewCheckBoxCell)(dtpData2.Rows[i].Cells[0])).Value = cbCheck2.Checked;
            }
        }
        #endregion

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 1)
            {
                //tabControl1.SelectedIndex = 1;
                //MessageBox.Show(tabControl1.TabPages[1].Text.ToString());
                //tabControl1.SelectedIndex = 0;
                try
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("id");//生产单id
                    dt.Columns.Add("prdnCode");
                    dt.Columns.Add("material");
                    dt.Columns.Add("materialCode");
                    dt.Columns.Add("materialName");
                    dt.Columns.Add("workstageid");
                    dt.Columns.Add("workstage");
                    dt.Columns.Add("specs");
                    dt.Columns.Add("dosage");
                    dt.Columns.Add("prdnMaterialId");

                    for (int i = 0; i < this.dtpData.Rows.Count; i++)
                    {
                        DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)(dtpData.Rows[i].Cells[0]);
                        if (c.Value != null && (bool)c.Value)
                        {
                            XPrdnWorkstage[] w = BaseDataProxy.GetXPrdnWorkstages(Convert.ToInt32(this.dtpData.RCText(i, "id")));//跟据明细id得到对应工序

                            for (int j = 0; j < w.Count(); j++)
                            {
                                XPrdnMaterial[] m = BaseDataProxy.GetXPrdnMaterials(w[j].PrdnWorkstage.id);//跟据工序id得到对应的生产材料
                                for (int k = 0; k < m.Count(); k++)
                                {
                                    DataRow dr = dt.NewRow();
                                    dr["id"] = Convert.ToInt32(this.dtpData.RCText(i, "id"));//生产单id
                                    dr["prdnCode"]=this.dtpData.RCText(i, "code");//生产单号
                                    dr["material"]=m[k].PrdnMaterial.material;
                                    dr["materialCode"] = m[k].MaterialCode;
                                    dr["materialName"]=m[k].MaterialName;
                                    dr["workstageid"] = w[j].PrdnWorkstage.id;//工序id
                                    dr["workstage"]=w[j].WorkstageCode;//工序号
                                    dr["specs"]=m[k].Specs;
                                    dr["dosage"] = m[k].PrdnMaterial.dosage;
                                    dr["prdnMaterialId"] = m[k].PrdnMaterial.id;//生产材料id
                                    dt.Rows.Add(dr);
                                }
                            }
                        }
                    }

                    this.dtpData2.DataSource = dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {                
            try
            {
                for (int i = 0; i < this.dtpData2.Rows.Count; i++)
                {
                    DataGridViewCheckBoxCell c = (DataGridViewCheckBoxCell)(dtpData2.Rows[i].Cells[0]);
                    if (c.Value != null && (bool)c.Value)
                    {
                        SelectMaterialSpecFrm dlg = new SelectMaterialSpecFrm();
                        dlg.material = Convert.ToInt32(this.dtpData2.RCText(i, "material"));
                        dlg.materialCode = this.dtpData2.RCText(i, "t2");
                        dlg.materialName = this.dtpData2.RCText(i, "t4");
                        dlg.specs = this.dtpData2.RCText(i, "t5");
                        dlg.Num = this.dtpData2.RCText(i, "t6");
                        dlg.gxid = Convert.ToInt32(this.dtpData2.RCText(i, "t9"));//生产工序id
                        dlg.gx = this.dtpData2.RCText(i, "t3");//生产工序
                        dlg.scdid = Convert.ToInt32(this.dtpData2.RCText(i, "t8"));//生产单id
                        dlg.scd = this.dtpData2.RCText(i, "t1");//生产单号
                        dlg.prdnMaterialId = Convert.ToInt32(this.dtpData2.RCText(i, "t10"));//生产材料id
                        dlg.ShowDialog();
                        //XStock[] xs = BaseDataProxy.GetBatchStocks(1);

                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
